# -*- coding: utf-8 -*-

from typing import Literal

FILL = "resizemode.FILL"
"""填充"""

ADAPT = "resizemode.ADAPT"
"""适应"""

DITHERING_BW = "bwize.DITHERING"
"""抖动黑白"""

BINARY_BW = "bwize.BINARY"
"""二值化黑白"""

GRAY_BW = "bwize.GRAY"
"""灰度黑白"""

RESIZEMODE_TYPE = Literal["resizemode.FILL", "resizemode.ADAPT"]
BWIZE_TYPE = Literal["bwize.DITHERING", "bwize.BINARY", "bwize.GRAY"]

FILE_SAVE_EXTENSION = FILE_SAVE_SUFFIX = "MVSP"
"""文件缓存拓展名"""


MC_RGB_DROP_LIST = [
    "§0",
    "§1",
    "§2",
    "§3",
    "§t",
    "§s",
    "§j",
    "§q",
    "§8",
    "§9",
    "§a",
    "§b",
    "§m",
    "§u",
    "§4",
    "§5",
    "§7",
    "§n",
    "§i",
    "§p",
    "§g",
    "§h",
    "§c",
    "§d",
    "§6",
    "§e",
    "§f",
    "§e",
    "§6",
    "§d",
    "§c",
    "§h",
    "§g",
    "§p",
    "§i",
    "§n",
    "§7",
    "§5",
    "§4",
    "§u",
    "§m",
    "§b",
    "§a",
    "§9",
    "§8",
    "§q",
    "§j",
    "§s",
    "§t",
    "§3",
    "§2",
    "§1",
]

MC_STABLE_RGB_TABLE = {
    "000000": "§0",
    "0000AA": "§1",
    "00AA00": "§2",
    "00AAAA": "§3",
    "AA0000": "§4",
    "AA00AA": "§5",
    "FFAA00": "§6",
    "AAAAAA": "§7",
    "555555": "§8",
    "5555FF": "§9",
    "55FF55": "§a",
    "55FFFF": "§b",
    "FF5555": "§c",
    "FF55FF": "§d",
    "FFFF55": "§e",
    "FFFFFF": "§f",
    "DDD605": "§g",
    "E3D4D1": "§h",
    "CECACA": "§i",
    "443A3B": "§j",
    "971607": "§m",
    "B4684D": "§n",
    "DEB12D": "§p",
    "47A036": "§q",
    "2CBAA8": "§s",
    "21497B": "§t",
    "9A5CC6": "§u",
}

RGB_CHAR_TABLE = {
    (0, 0, 0): ("§0", "▏"),
    (11, 11, 11): ("§0", "╏"),
    (18, 18, 18): ("§0", "┇"),
    (24, 24, 24): ("§0", "┋"),
    (0, 0, 170): ("§1", "▏"),
    (11, 11, 148): ("§1", "╏"),
    (18, 18, 137): ("§1", "┇"),
    (24, 24, 126): ("§1", "┋"),
    (0, 170, 0): (" §2", "▏"),
    (11, 148, 11): ("§2", "╏"),
    (18, 137, 18): ("§2", "┇"),
    (24, 126, 24): ("§2", "┋"),
    (0, 170, 170): ("§3", "▏"),
    (11, 148, 148): ("§3", "╏"),
    (18, 137, 137): ("§3", "┇"),
    (24, 126, 126): ("§3", "┋"),
    (170, 0, 0): ("§4", "▏"),
    (148, 11, 11): ("§4", "╏"),
    (137, 18, 18): ("§4", "┇"),
    (126, 24, 24): ("§4", "┋"),
    (170, 0, 170): ("§5", "▏"),
    (148, 11, 148): ("§5", "╏"),
    (137, 18, 137): ("§5", "┇"),
    (126, 24, 126): ("§5", "┋"),
    (255, 170, 0): ("§6", "▏"),
    (216, 148, 11): ("§6", "╏"),
    (196, 137, 18): ("§6", "┇"),
    (177, 126, 24): ("§6", "┋"),
    (170, 170, 170): ("§7", "▏"),
    (148, 148, 148): ("§7", "╏"),
    (137, 137, 137): ("§7", "┇"),
    (126, 126, 126): ("§7", "┋"),
    (85, 85, 85): ("§8", "▏"),
    (80, 80, 80): ("§8", "╏"),
    (77, 77, 77): ("§8", "┇"),
    (75, 75, 75): ("§8", "┋"),
    (85, 85, 255): ("§9", "▏"),
    (80, 80, 216): ("§9", "╏"),
    (77, 77, 196): ("§9", "┇"),
    (75, 75, 177): ("§9", "┋"),
    (85, 255, 85): ("§a", "▏"),
    (80, 216, 80): ("§a", "╏"),
    (77, 196, 77): ("§a", "┇"),
    (75, 177, 75): ("§a", "┋"),
    (85, 255, 255): ("§b", "▏"),
    (80, 216, 216): ("§b", "╏"),
    (77, 196, 196): ("§b", "┇"),
    (75, 177, 177): ("§b", "┋"),
    (255, 85, 85): ("§c", "▏"),
    (216, 80, 80): ("§c", "╏"),
    (196, 77, 77): ("§c", "┇"),
    (177, 75, 75): ("§c", "┋"),
    (255, 85, 255): ("§d", "▏"),
    (216, 80, 216): ("§d", "╏"),
    (196, 77, 196): ("§d", "┇"),
    (177, 75, 177): ("§d", "┋"),
    (255, 255, 85): ("§e", "▏"),
    (216, 216, 80): ("§e", "╏"),
    (196, 196, 77): ("§e", "┇"),
    (177, 177, 75): ("§e", "┋"),
    (255, 255, 255): ("§f", "▏"),
    (216, 216, 216): ("§f", "╏"),
    (196, 196, 196): ("§f", "┇"),
    (177, 177, 177): ("§f", "┋"),
    (221, 214, 5): ("§g", "▏"),
    (188, 183, 15): ("§g", "╏"),
    (172, 167, 21): ("§g", "┇"),
    (156, 152, 27): ("§g", "┋"),
    (227, 212, 209): ("§h", "▏"),
    (193, 181, 179): ("§h", "╏"),
    (176, 166, 164): ("§h", "┇"),
    (160, 151, 149): ("§h", "┋"),
    (206, 202, 202): ("§i", "▏"),
    (176, 173, 173): ("§i", "╏"),
    (162, 159, 159): ("§i", "┇"),
    (147, 145, 145): ("§i", "┋"),
    (68, 58, 59): ("§j", "▏"),
    (66, 58, 59): ("§j", "╏"),
    (65, 58, 59): ("§j", "┇"),
    (64, 58, 59): ("§j", "┋"),
    (151, 22, 7): ("§m", "▏"),
    (132, 29, 17): ("§m", "╏"),
    (123, 33, 22): ("§m", "┇"),
    (114, 37, 28): ("§m", "┋"),
    (180, 104, 77): ("§n", "▏"),
    (156, 95, 73): ("§n", "╏"),
    (144, 90, 71): ("§n", "┇"),
    (132, 86, 70): ("§n", "┋"),
    (222, 177, 45): ("§p", "▏"),
    (189, 153, 48): ("§p", "╏"),
    (173, 141, 49): ("§p", "┇"),
    (157, 130, 51): ("§p", "┋"),
    (17, 160, 54): ("§q", "▏"),
    (25, 140, 55): ("§q", "╏"),
    (29, 130, 55): ("§q", "┇"),
    (34, 120, 56): ("§q", "┋"),
    (44, 186, 168): ("§s", "▏"),
    (47, 160, 146): ("§s", "╏"),
    (48, 148, 135): ("§s", "┇"),
    (50, 135, 124): ("§s", "┋"),
    (33, 73, 123): ("§t", "▏"),
    (38, 70, 110): ("§t", "╏"),
    (41, 69, 104): ("§t", "┇"),
    (43, 67, 97): ("§t", "┋"),
    (154, 92, 198): ("§u", "▏"),
    (135, 85, 170): ("§u", "╏"),
    (125, 82, 156): ("§u", "┇"),
    (116, 79, 142): ("§u", "┋"),
}


CVT_PALETTE = [
    0,
    0,
    0,
    11,
    11,
    11,
    18,
    18,
    18,
    24,
    24,
    24,
    0,
    0,
    170,
    11,
    11,
    148,
    18,
    18,
    137,
    24,
    24,
    126,
    0,
    170,
    0,
    11,
    148,
    11,
    18,
    137,
    18,
    24,
    126,
    24,
    0,
    170,
    170,
    11,
    148,
    148,
    18,
    137,
    137,
    24,
    126,
    126,
    170,
    0,
    0,
    148,
    11,
    11,
    137,
    18,
    18,
    126,
    24,
    24,
    170,
    0,
    170,
    148,
    11,
    148,
    137,
    18,
    137,
    126,
    24,
    126,
    255,
    170,
    0,
    216,
    148,
    11,
    196,
    137,
    18,
    177,
    126,
    24,
    170,
    170,
    170,
    148,
    148,
    148,
    137,
    137,
    137,
    126,
    126,
    126,
    85,
    85,
    85,
    80,
    80,
    80,
    77,
    77,
    77,
    75,
    75,
    75,
    85,
    85,
    255,
    80,
    80,
    216,
    77,
    77,
    196,
    75,
    75,
    177,
    85,
    255,
    85,
    80,
    216,
    80,
    77,
    196,
    77,
    75,
    177,
    75,
    85,
    255,
    255,
    80,
    216,
    216,
    77,
    196,
    196,
    75,
    177,
    177,
    255,
    85,
    85,
    216,
    80,
    80,
    196,
    77,
    77,
    177,
    75,
    75,
    255,
    85,
    255,
    216,
    80,
    216,
    196,
    77,
    196,
    177,
    75,
    177,
    255,
    255,
    85,
    216,
    216,
    80,
    196,
    196,
    77,
    177,
    177,
    75,
    255,
    255,
    255,
    216,
    216,
    216,
    196,
    196,
    196,
    177,
    177,
    177,
    221,
    214,
    5,
    188,
    183,
    15,
    172,
    167,
    21,
    156,
    152,
    27,
    227,
    212,
    209,
    193,
    181,
    179,
    176,
    166,
    164,
    160,
    151,
    149,
    206,
    202,
    202,
    176,
    173,
    173,
    162,
    159,
    159,
    147,
    145,
    145,
    68,
    58,
    59,
    66,
    58,
    59,
    65,
    58,
    59,
    64,
    58,
    59,
    151,
    22,
    7,
    132,
    29,
    17,
    123,
    33,
    22,
    114,
    37,
    28,
    180,
    104,
    77,
    156,
    95,
    73,
    144,
    90,
    71,
    132,
    86,
    70,
    222,
    177,
    45,
    189,
    153,
    48,
    173,
    141,
    49,
    157,
    130,
    51,
    17,
    160,
    54,
    25,
    140,
    55,
    29,
    130,
    55,
    34,
    120,
    56,
    44,
    186,
    168,
    47,
    160,
    146,
    48,
    148,
    135,
    50,
    135,
    124,
    33,
    73,
    123,
    38,
    70,
    110,
    41,
    69,
    104,
    43,
    67,
    97,
    154,
    92,
    198,
    135,
    85,
    170,
    125,
    82,
    156,
    116,
    79,
    142,
    0,
    0,
    0,
    11,
    11,
    11,
    18,
    18,
    18,
    24,
    24,
    24,
    0,
    0,
    170,
    11,
    11,
    148,
    18,
    18,
    137,
    24,
    24,
    126,
    0,
    170,
    0,
    11,
    148,
    11,
    18,
    137,
    18,
    24,
    126,
    24,
    0,
    170,
    170,
    11,
    148,
    148,
    18,
    137,
    137,
    24,
    126,
    126,
    170,
    0,
    0,
    148,
    11,
    11,
    137,
    18,
    18,
    126,
    24,
    24,
    170,
    0,
    170,
    148,
    11,
    148,
    137,
    18,
    137,
    126,
    24,
    126,
    255,
    170,
    0,
    216,
    148,
    11,
    196,
    137,
    18,
    177,
    126,
    24,
    170,
    170,
    170,
    148,
    148,
    148,
    137,
    137,
    137,
    126,
    126,
    126,
    85,
    85,
    85,
    80,
    80,
    80,
    77,
    77,
    77,
    75,
    75,
    75,
    85,
    85,
    255,
    80,
    80,
    216,
    77,
    77,
    196,
    75,
    75,
    177,
    85,
    255,
    85,
    80,
    216,
    80,
    77,
    196,
    77,
    75,
    177,
    75,
    85,
    255,
    255,
    80,
    216,
    216,
    77,
    196,
    196,
    75,
    177,
    177,
    255,
    85,
    85,
    216,
    80,
    80,
    196,
    77,
    77,
    177,
    75,
    75,
    255,
    85,
    255,
    216,
    80,
    216,
    196,
    77,
    196,
    177,
    75,
    177,
    255,
    255,
    85,
    216,
    216,
    80,
    196,
    196,
    77,
    177,
    177,
    75,
    255,
    255,
    255,
    216,
    216,
    216,
    196,
    196,
    196,
    177,
    177,
    177,
    221,
    214,
    5,
    188,
    183,
    15,
    172,
    167,
    21,
    156,
    152,
    27,
    227,
    212,
    209,
    193,
    181,
    179,
    176,
    166,
    164,
    160,
    151,
    149,
    206,
    202,
    202,
    176,
    173,
    173,
    162,
    159,
    159,
    147,
    145,
    145,
    68,
    58,
    59,
    66,
    58,
    59,
    65,
    58,
    59,
    64,
    58,
    59,
    151,
    22,
    7,
    132,
    29,
    17,
    123,
    33,
    22,
    114,
    37,
    28,
    180,
    104,
    77,
    156,
    95,
    73,
    144,
    90,
    71,
    132,
    86,
    70,
    222,
    177,
    45,
    189,
    153,
    48,
    173,
    141,
    49,
    157,
    130,
    51,
    17,
    160,
    54,
    25,
    140,
    55,
    29,
    130,
    55,
    34,
    120,
    56,
    44,
    186,
    168,
    47,
    160,
    146,
    48,
    148,
    135,
    50,
    135,
    124,
    33,
    73,
    123,
    38,
    70,
    110,
    41,
    69,
    104,
    43,
    67,
    97,
    154,
    92,
    198,
    135,
    85,
    170,
    125,
    82,
    156,
    116,
    79,
    142,
    0,
    0,
    0,
    11,
    11,
    11,
    18,
    18,
    18,
    24,
    24,
    24,
    0,
    0,
    170,
    11,
    11,
    148,
    18,
    18,
    137,
    24,
    24,
    126,
    0,
    170,
    0,
    11,
    148,
    11,
    18,
    137,
    18,
    24,
    126,
    24,
    0,
    170,
    170,
    11,
    148,
    148,
    18,
    137,
    137,
    24,
    126,
    126,
    170,
    0,
    0,
    148,
    11,
    11,
    137,
    18,
    18,
    126,
    24,
    24,
    170,
    0,
    170,
    148,
    11,
    148,
    137,
    18,
    137,
    126,
    24,
    126,
    255,
    170,
    0,
    216,
    148,
    11,
    196,
    137,
    18,
    177,
    126,
    24,
    170,
    170,
    170,
    148,
    148,
    148,
    137,
    137,
    137,
    126,
    126,
    126,
    85,
    85,
    85,
    80,
    80,
    80,
    77,
    77,
    77,
    75,
    75,
    75,
    85,
    85,
    255,
    80,
    80,
    216,
    77,
    77,
    196,
    75,
    75,
    177,
]

from PIL import Image, ImageFont

PALETTE_IMAGE = Image.new("P", (16, 16))
PALETTE_IMAGE.putpalette(CVT_PALETTE)

MC_FONT_PATH = "./src/unifont.ttf"
MC_FONT_SIZE = 16

MC_FONT = ImageFont.truetype(MC_FONT_PATH, MC_FONT_SIZE)


RGB_CHAR_TABLE2 = {
    (48, 48, 48): ("§0", "0", (0, 0, 0)),
    (0, 0, 0): ("§0", "1", (48, 48, 48)),
    (48, 48, 82): ("§1", "0", (0, 0, 170)),
    (0, 0, 170): ("§1", "1", (48, 48, 82)),
    (48, 82, 48): ("§2", "0", (0, 170, 0)),
    (0, 170, 0): ("§2", "1", (48, 82, 48)),
    (48, 82, 82): ("§3", "0", (0, 170, 170)),
    (0, 170, 170): ("§3", "1", (48, 82, 82)),
    (82, 48, 48): ("§4", "0", (170, 0, 0)),
    (170, 0, 0): ("§4", "1", (82, 48, 48)),
    (82, 48, 82): ("§5", "0", (170, 0, 170)),
    (170, 0, 170): ("§5", "1", (82, 48, 82)),
    (99, 82, 48): ("§6", "0", (255, 170, 0)),
    (255, 170, 0): ("§6", "1", (99, 82, 48)),
    (82, 82, 82): ("§7", "0", (170, 170, 170)),
    (170, 170, 170): ("§7", "1", (82, 82, 82)),
    (65, 65, 65): ("§8", "0", (85, 85, 85)),
    (85, 85, 85): ("§8", "1", (65, 65, 65)),
    (65, 65, 99): ("§9", "0", (85, 85, 255)),
    (85, 85, 255): ("§9", "1", (65, 65, 99)),
    (65, 99, 65): ("§a", "0", (85, 255, 85)),
    (85, 255, 85): ("§a", "1", (65, 99, 65)),
    (65, 99, 99): ("§b", "0", (85, 255, 255)),
    (85, 255, 255): ("§b", "1", (65, 99, 99)),
    (99, 65, 65): ("§c", "0", (255, 85, 85)),
    (255, 85, 85): ("§c", "1", (99, 65, 65)),
    (99, 65, 99): ("§d", "0", (255, 85, 255)),
    (255, 85, 255): ("§d", "1", (99, 65, 99)),
    (99, 99, 65): ("§e", "0", (255, 255, 85)),
    (255, 255, 85): ("§e", "1", (99, 99, 65)),
    (99, 99, 99): ("§f", "0", (255, 255, 255)),
    (255, 255, 255): ("§f", "1", (99, 99, 99)),
    (92, 90, 49): ("§g", "0", (221, 214, 5)),
    (221, 214, 5): ("§g", "1", (92, 90, 49)),
    (93, 90, 89): ("§h", "0", (227, 212, 209)),
    (227, 212, 209): ("§h", "1", (93, 90, 89)),
    (89, 88, 88): ("§i", "0", (206, 202, 202)),
    (206, 202, 202): ("§i", "1", (89, 88, 88)),
    (61, 59, 59): ("§j", "0", (68, 58, 59)),
    (68, 58, 59): ("§j", "1", (61, 59, 59)),
    (78, 52, 49): ("§m", "0", (151, 22, 7)),
    (151, 22, 7): ("§m", "1", (78, 52, 49)),
    (84, 68, 63): ("§n", "0", (180, 104, 77)),
    (180, 104, 77): ("§n", "1", (84, 68, 63)),
    (92, 83, 57): ("§p", "0", (222, 177, 45)),
    (222, 177, 45): ("§p", "1", (92, 83, 57)),
    (51, 80, 58): ("§q", "0", (17, 160, 54)),
    (17, 160, 54): ("§q", "1", (51, 80, 58)),
    (56, 85, 81): ("§s", "0", (44, 186, 168)),
    (44, 186, 168): ("§s", "1", (56, 85, 81)),
    (54, 62, 72): ("§t", "0", (33, 73, 123)),
    (33, 73, 123): ("§t", "1", (54, 62, 72)),
    (78, 66, 87): ("§u", "0", (154, 92, 198)),
    (154, 92, 198): ("§u", "1", (78, 66, 87)),
}

CVT_PALETTE2 = [
    48,
    48,
    48,
    0,
    0,
    0,
    48,
    48,
    82,
    0,
    0,
    170,
    48,
    82,
    48,
    0,
    170,
    0,
    48,
    82,
    82,
    0,
    170,
    170,
    82,
    48,
    48,
    170,
    0,
    0,
    82,
    48,
    82,
    170,
    0,
    170,
    99,
    82,
    48,
    255,
    170,
    0,
    82,
    82,
    82,
    170,
    170,
    170,
    65,
    65,
    65,
    85,
    85,
    85,
    65,
    65,
    99,
    85,
    85,
    255,
    65,
    99,
    65,
    85,
    255,
    85,
    65,
    99,
    99,
    85,
    255,
    255,
    99,
    65,
    65,
    255,
    85,
    85,
    99,
    65,
    99,
    255,
    85,
    255,
    99,
    99,
    65,
    255,
    255,
    85,
    99,
    99,
    99,
    255,
    255,
    255,
    92,
    90,
    49,
    221,
    214,
    5,
    93,
    90,
    89,
    227,
    212,
    209,
    89,
    88,
    88,
    206,
    202,
    202,
    61,
    59,
    59,
    68,
    58,
    59,
    78,
    52,
    49,
    151,
    22,
    7,
    84,
    68,
    63,
    180,
    104,
    77,
    92,
    83,
    57,
    222,
    177,
    45,
    51,
    80,
    58,
    17,
    160,
    54,
    56,
    85,
    81,
    44,
    186,
    168,
    54,
    62,
    72,
    33,
    73,
    123,
    78,
    66,
    87,
    154,
    92,
    198,
]

# img_to_title 调色板2
MC_IMAGE_PALETTE2 = Image.new("P", (16, 16))
MC_IMAGE_PALETTE2.putpalette(CVT_PALETTE2)
